import { useMutation } from "@apollo/client";
import { login_service, register_service } from "../../graphql/login";

export const useFetch_register = () => {
  const [fun, { loading }] = useMutation(register_service);

  const register = async (account: string, password: string) => {
    const res = await fun({
      variables: {
        params: {
          account,
          password,
        },
      },
    });
    return res.data.createStudent as { code: boolean; message: string };
  };

  return {
    register,
    registerLoding: loading,
  };
};

export const useFetch_login = () => {
  const [fun, { loading }] = useMutation(login_service);

  const login = async (account: string, password: string) => {
    const res = await fun({
      variables: {
        account,
        password,
        isStudent: true,
      },
    });

    return res.data.login as { code: boolean; message: string; data: string };
  };

  return { login, loginLoading: loading };
};
